package com.itcast.googleplay;

import com.itcast.googleplay.factory.FragmentFactory;
import com.itcast.googleplay.fragment.BaseFragment;
import com.itcast.googleplay.ui.PagerTab;
import com.itcast.googleplay.utils.UIUtils;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.view.Gravity;
import android.view.MenuItem;

public class MainActivity extends BaseActivity {
    private DrawerLayout drawerLayout;
	private ActionBar supportActionBar;
	private ActionBarDrawerToggle actionBarDrawerToggle;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        initActionBar();
        
        PagerTab pagerTab = (PagerTab) findViewById(R.id.pagerTab);
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        
        pagerTab.setViewPager(viewPager);
        
        pagerTab.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				BaseFragment baseFragment = FragmentFactory.getFragment(position);
				baseFragment.showData();
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
    }
    private void initActionBar() {
    	supportActionBar = getSupportActionBar();
    	supportActionBar.setTitle("谷歌市场");
    	supportActionBar.setIcon(R.drawable.ic_launcher);
    	
    	supportActionBar.setDisplayHomeAsUpEnabled(true);//左上角按钮可以点击
    	supportActionBar.setDisplayShowHomeEnabled(true);//显示左上角按钮
    	
    	actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer_am, 0, 0);
    	actionBarDrawerToggle.syncState();
    	drawerLayout.setDrawerListener(actionBarDrawerToggle);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
		case android.R.id.home:
//			if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
//				drawerLayout.closeDrawer(Gravity.LEFT);
//			}else{
//				drawerLayout.openDrawer(Gravity.LEFT);
//			}
			actionBarDrawerToggle.onOptionsItemSelected(item);
			break;
		}
    	return super.onOptionsItemSelected(item);
    }
    
	class MyFragmentPagerAdapter extends FragmentPagerAdapter{
		private String[] tabStrings;

		public MyFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
			tabStrings = UIUtils.getStringArray(R.array.tab_names);
		}
		@Override
		public Fragment getItem(int position) {
			BaseFragment fragment = FragmentFactory.getFragment(position);
			return fragment;
		}
		@Override
		public int getCount() {
			return tabStrings.length;
		}
		@Override
		public CharSequence getPageTitle(int position) {
			return tabStrings[position];
		}
    }
}
